package controler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.Agreement;
import model.beans.AcademiaBean;
import model.beans.AcuerdoBean;
import model.beans.MinutaBean;
import model.beans.UsuarioBean;
import business.misc.Util;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.MappingDispatchAction;

import java.util.Vector;

public class AgreementAction extends MappingDispatchAction {
	private final String DEFAULT = "default";
	
	/**
	 * Listado de Acuerdos por minuta
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 * 
	 * @author Luis Celis
	 */
	public ActionForward list(
			ActionMapping mapping, 
			ActionForm form, 
			HttpServletRequest request, 
			HttpServletResponse response) throws Exception {
			
			ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
			if(action_retVal!=null)return action_retVal;
		
			HttpSession session = request.getSession();
			UsuarioBean user_bn = (UsuarioBean)session.getAttribute("ses_usuario");
			AcademiaBean academia=user_bn.getAcademia();
			 String str_academyCode = academia.getCodigo();
			 
			 
			 MinutaBean minuta_bean = (MinutaBean) session.getAttribute("bean_minuta");
			 			
			Vector arrL_acuerdo = business.Acuerdo.list(minuta_bean.getInt_tk(), str_academyCode);
			request.setAttribute("lst_acuerdo", arrL_acuerdo );
		
			return mapping.findForward(DEFAULT);
	}
	
	public ActionForward save(
			ActionMapping mapping, 
			ActionForm form, 
			HttpServletRequest request, 
			HttpServletResponse response) throws Exception {
		
			ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
			if(action_retVal!=null)return action_retVal;
			
			String str_forward;
			
			HttpSession session = request.getSession();
			MinutaBean minuta_bn = (MinutaBean)session.getAttribute("bean_minuta");
			
			UsuarioBean user_bn = (UsuarioBean)session.getAttribute("ses_usuario");
			AcademiaBean academia=user_bn.getAcademia();
			String str_academyCode = academia.getCodigo();
			
			DynaActionForm forma = (DynaActionForm)form;
				
			if( ((Integer)forma.get("int_id")).intValue() == 0 ){
				if( model.Agreement.add(forma.getMap(),minuta_bn.getInt_tk()) > 0 ){
					request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.SuccessAdd"));
					str_forward = Util.SUCCESS;
				}else{
					request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.FailAdd"));
					str_forward = Util.FAIL+Util.ADD;
				}
			}else{
				if( model.Agreement.edit(forma.getMap()) > 0 ){
					request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.SuccessEdit"));
					str_forward = Util.SUCCESS;
				}else{
					request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.FailAdd"));
					str_forward = Util.FAIL+Util.EDIT;
				}
			}
			
			if(str_forward.equals(Util.SUCCESS)){
				request.setAttribute("lst_acuerdo", business.Acuerdo.list(minuta_bn.getInt_tk(), str_academyCode));				
			}
			request.setAttribute("draft_id", forma.get("int_minuta"));
		return mapping.findForward(str_forward);
	}
	
	public ActionForward edit(
			ActionMapping mapping, 
			ActionForm form, 
			HttpServletRequest request, 
			HttpServletResponse response) throws Exception {
			
			ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
			if(action_retVal!=null)return action_retVal;
		
			HttpSession session = request.getSession();
			UsuarioBean user_bn = (UsuarioBean)session.getAttribute("ses_usuario");
			AcademiaBean academia=user_bn.getAcademia();
			String str_academyCode = academia.getCodigo();
		
			String acuerdo_id = request.getParameter("acuerdo_id");
			
			DynaActionForm forma = (DynaActionForm)form;
			
			AcuerdoBean bn_acuerdo = Agreement.instance(Integer.parseInt(acuerdo_id), str_academyCode);
			
			forma.set("int_minuta", new Integer(bn_acuerdo.getInt_minuta()));
			forma.set("int_acuerdo", new Integer(bn_acuerdo.getInt_acuerdo()));
			forma.set("str_acuerdo", bn_acuerdo.getStr_acuerdo());
			forma.set("int_responsable", new Integer(bn_acuerdo.getInt_responsable()));
			forma.set("str_responsable",bn_acuerdo.getStr_responsable());
			forma.set("str_fecha", bn_acuerdo.getStr_fecha());
			forma.set("str_producto", bn_acuerdo.getStr_producto());
			forma.set("int_id", new Integer( bn_acuerdo.getInt_id()));
			
		return mapping.findForward(DEFAULT);
	}
	
	public ActionForward add(
			ActionMapping mapping, 
			ActionForm form, 
			HttpServletRequest request, 
			HttpServletResponse response) throws Exception {
		
			ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
			if(action_retVal!=null)return action_retVal;
		
			DynaActionForm forma = (DynaActionForm)form;
			forma.initialize(mapping);
			forma.reset(mapping, request);
			
		return mapping.findForward(DEFAULT);
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
		if(action_retVal!=null)return action_retVal;		
		
		HttpSession session = request.getSession();
		MinutaBean minuta_bn = (MinutaBean)session.getAttribute("bean_minuta");
		
		UsuarioBean user_bn = (UsuarioBean)session.getAttribute("ses_usuario");
		AcademiaBean academia=user_bn.getAcademia();
		String str_academyCode = academia.getCodigo();
		
		String acuerdo_id = request.getParameter("acuerdo_id");
		
		if( Agreement.delete(Integer.parseInt(acuerdo_id))){
			request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.SuccessDelete"));
		}else{
			request.setAttribute("req_msg", getResources(request).getMessage("Agreement.FailDelete"));
		}
		request.setAttribute("lst_acuerdo", business.Acuerdo.list(minuta_bn.getInt_tk(), str_academyCode));
		return mapping.findForward(Util.DEFAULT);
	}
		
}
